package cn.stylefeng.guns.modular.order.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.order.model.Order;
import cn.stylefeng.guns.modular.order.service.*;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.warpper.RoleWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * order控制器
 *
 * @author yangyu
 * @Date 2019-12-10 10:37:14
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private String PREFIX = "/modular/order/";

    @Autowired
    private IOrderService orderService;

    /**
     * 跳转到order首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "order.html";
    }

    /**
     * 跳转到添加order
     */
    @RequestMapping("/order_add")
    public String orderAdd() {
        return PREFIX + "order_add.html";
    }

    /**
     * 跳转到修改order
     */
    @RequestMapping("/order_update")
    public String orderUpdate(Integer orderId, Model model) {
        Order order = orderService.getById(orderId);
        model.addAttribute("item",order);
        LogObjectHolder.me().set(order);
        return PREFIX + "order_edit.html";
    }

    /**
     * 获取order列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<Map<String, Object>> orders = orderService.selectOrders(null);
        return LayuiPageFactory.createPageInfo(orders);
    }

    /**
     * 新增order
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Order order) {
        orderService.saveOrder(order);
        return SUCCESS_TIP;
    }

    /**
     * 删除order
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer orderId) {
        orderService.removeById(orderId);
        return SUCCESS_TIP;
    }

    /**
     * 修改order
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Order order) {
        orderService.updateById(order);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/view/{orderId}")
    @ResponseBody
    public ResponseData view(@PathVariable Long orderId) {
        if (ToolUtil.isEmpty(orderId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Order order = orderService.getById(orderId);
        Map<String, Object> roleMap = BeanUtil.beanToMap(order);

        return ResponseData.success(roleMap);
    }



    /**
     * order详情
     */
    @RequestMapping(value = "/detail/{orderId}")
    @ResponseBody
    public Object detail(@PathVariable("orderId") Integer orderId) {
        return orderService.getById(orderId);
    }
}
